import { Vector3 } from "@minecraft/server";
import { IDynamicPropertyTarget } from "./idynamic_property_target";

export type DynamicPropertyValueType = boolean | number | string | Vector3;

export class DynamicData<T extends DynamicPropertyValueType> {
  constructor(
    private readonly target: IDynamicPropertyTarget,
    private readonly id: string,
    value?: T
  ) {
    if (value == undefined) return;
    if (this.get() == undefined) {
      this.set(value);
    }
  }
  get(): T | undefined {
    return <T>this.target.getDynamicProperty(this.id);
  }
  set(value: T): void {
    this.target.setDynamicProperty(this.id, value);
  }
  free(): void {
    this.target.setDynamicProperty(this.id, undefined);
  }
  getId(): string {
    return this.id;
  }
}
